﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Windows.Resources;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class Tooltips : Window, IWindow
    {     
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion
        public void SetTheme()
        {
            pivotGrid1.FontFamily = new FontFamily("Tahoma");
        }
  
        public Tooltips()
        {
            InitializeComponent();

            PrepareGridData();
        
            pivotGrid1.PivotValuesOnRows = false;
            pivotGrid1.RowsHierarchy.CompactStyleRenderingEnabled = true;
            pivotGrid1.ItemsSource = lst;

            pivotGrid1.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ALL);
            pivotGrid1.RowsHierarchy.SetColumnWidth(0, 150);

            // Enable Tooltips
            pivotGrid1.ToolTipsEnabled = true;

            // Set the initial show delay
            pivotGrid1.ToolTipShowDelay = TimeSpan.FromSeconds(2);

            // Set the tooltip duration time
            pivotGrid1.ToolTipDuration = TimeSpan.FromSeconds(5);

            // Handle the TooltipShow event. This allows for toolip content customization at runtime
            pivotGrid1.ToolTipShow += new VIBlend.WPF.Controls.DataGrid.ToolTipEventHandler(pivotGrid1_TooltipShow);

            this.pivotGrid1.Refresh();
        }

        void pivotGrid1_TooltipShow(object sender, VIBlend.WPF.Controls.ToolTipEventArgs args)
        {
            // If the row item contains 'France' change the text of the tooltip
            if (args.RowItem != null && args.RowItem.Text.Contains("France"))
            {
                args.ToolTipContent = "This is a custom tooltip text";
                args.Handled = true;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.lst.Clear();
            this.lst = null;
            base.OnClosing(e);
            this.pivotGrid1.ToolTipShow -= new VIBlend.WPF.Controls.DataGrid.ToolTipEventHandler(pivotGrid1_TooltipShow);
            this.pivotGrid1.Dispose();
            this.pivotGrid1 = null;
        }

        #region Sample Data

        public class Invoice
        {
            public Invoice(
                string City,
                string Region,
                string Country,
                string SalesPerson,
                string ShipperCompany,
                string ProductName,
                double UnitPrice,
                double Quantity,
                double Discount,
                double ExtendedPrice,
                double Freight)
            {
                this.SalesPerson = SalesPerson;
                this.City = City;
                this.Region = string.IsNullOrEmpty(Region) ? "All Regions" : Region;
                this.Country = Country;
                this.ShipperCompany = ShipperCompany;
                this.ProductName = ProductName;
                this.UnitPrice = UnitPrice;
                this.Quantity = Quantity;
                this.Discount = Discount;
                this.ExtendedPrice = ExtendedPrice;
                this.Freight = Freight;
            }

            public string SalesPerson { get; set; }
            public string City { get; set; }
            public string Region { get; set; }
            public string Country { get; set; }
            public string ShipperCompany { get; set; }
            public string ProductName { get; set; }
            public double UnitPrice { get; set; }
            public double Quantity { get; set; }
            public double Discount { get; set; }
            public double ExtendedPrice { get; set; }
            public double Freight { get; set; }
        }

        public List<Invoice> lst = new List<Invoice>();

        private void PrepareGridData()
        {
            try
            {
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("ExamplesCSharp;component/CSharp/DataGridViewDemos/invoices.tab", UriKind.Relative));
                System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
                string line = null;
                sr.ReadLine();
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tabs = line.Split('\t');
                    lst.Add(new Invoice(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], double.Parse(tabs[6]), double.Parse(tabs[7]), double.Parse(tabs[8]), double.Parse(tabs[9]), double.Parse(tabs[10])));
                }
                sr.Close();
            }
            catch (System.Exception)
            {
            }
        }
        #endregion

    }

}
